/**
 * Copyright (c) 2010-2011 julewa.com.  
 * All rights reserved. 
 * 
 * @author Huang Benhua
 * @date 2011-7-2
 * 
**/
package com.julewa;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LOG {
	public static final int TRACE = 0;
	public static final int DEBUG = 1;
	public static final int INFO = 2;
	public static final int WARN = 3;
	public static final int ERROR = 4;
	public static final int FATAL = 5;
	public static final int CLOSE = 6;
	public int getLevel(){
		return level;
	}
	private int level = INFO;
	private Log logger = null;
	public LOG(Class<?> clazz){
		logger = LogFactory.getLog(clazz);
		//
		if(logger.isTraceEnabled()){
			level = TRACE;
		}else if(logger.isDebugEnabled()){
			level = DEBUG;
		}else if(logger.isInfoEnabled()){
			level = INFO;
		}else if(logger.isWarnEnabled()){
			level = WARN;
		}else if(logger.isErrorEnabled()){
			level = ERROR;
		}else if(logger.isFatalEnabled()){
			level = FATAL;
		}else{
			level = CLOSE;
		}
	
	}

	private String fmt(String m, Object...args){
		if(args==null || args.length < 1)return m;
		return String.format(m, args);
	}

	public void trace(Object msg){
		if(level > TRACE)return;
		logger.trace(msg);
	}
	
	public void debug(Object msg){
		if(level > DEBUG)return;
		logger.debug(msg);
	}
	
	public void info(Object msg){
		if(level > INFO)return;
		logger.info(msg);
	}
	
	public void warn(Object msg){
		if(level > WARN)return;
		logger.warn(msg);
	}
	
	public void warn(Object msg, Throwable err){
		if(level > WARN)return;
		logger.warn(msg, err);
	}
	
	public void error(Object msg){
		if(level > ERROR)return;
		logger.error(msg);
	}

	public void error(Object msg, Throwable err){
		if(level > WARN)return;
		logger.error(msg, err);
	}
	
	public void fatal(Object msg){
		if(level > FATAL)return;
		logger.fatal(msg);
	}

	public void fatal(Object msg, Throwable err){
		if(level > FATAL)return;
		logger.fatal(msg, err);
	}

	//////////////////////
	public void debug(String msg, Object ... args){
		if(level > DEBUG)return;
		logger.debug(fmt(msg, args));
	}
	
	public void trace(String msg, Object ... args){
		if(level > TRACE)return;
		logger.trace(fmt(msg, args));
	}
	
	public void info(String msg, Object ... args){
		if(level > INFO)return;
		logger.info(fmt(msg, args));
	}
	
	public void warn(String msg, Object ... args){
		if(level > WARN)return;
		logger.warn(fmt(msg, args));
	}
	
	public void warn(String msg, Throwable err, Object ... args){
		if(level > WARN)return;
		logger.warn(fmt(msg, args), err);
	}
	
	public void error(String msg, Object ... args){
		if(level > ERROR)return;
		logger.error(fmt(msg, args));
	}
	
	public void error(String msg, Throwable err, Object ... args){
		if(level > ERROR)return;
		logger.error(fmt(msg, args), err);
	}
	
	public void fatal(String msg, Object ... args){
		if(level > FATAL)return;
		logger.fatal(fmt(msg, args));
	}
	
	public void fatal(String msg, Throwable err, Object ... args){
		if(level > FATAL)return;
		logger.fatal(fmt(msg, args), err);
	}
	
}
